<?php

class Joob_Http_Response_Abstract {
	
	protected $body;
	
	protected $exceptions;
	
	protected $showException;
	
	public function __construct() {
		$this->exceptions = array ();
		$this->body = "";
		$this->showException = false;
	}
	
	/**
	 * Add exception to this response
	 */
	public function setException($ex) {
		$this->exceptions [] = $ex;
	}
	
	/**
	 * Get all exceptions
	 */
	public function getExceptions() {
		return $this->exceptions;
	}
	
	/**
	 * Get the current response body
	 */
	public function getBody() {
		return $this->body;
	}
	
	/**
	 * Output the current body
	 */
	public function outputBody() {
		echo $this->body;
	}
	
	/**
	 * Send the response, including the exceptions if requested
	 */
	public function sendResponse() {
		if ($this->showException && $this->isException ()) {
			foreach ( $this->exceptions as $ex ) {
				$exceptions = '';
				foreach ( $this->getExceptions () as $e ) {
					$exceptions .= $e->__toString () . "\n";
				}
				echo $exceptions;
				return;
			}
		}
		$this->outputBody ();
	}
	
	/**
	 * Should we show the exception
	 * @param bool $boolean
	 */
	public function showExceptions($boolean = true) {
		$this->showException = $boolean;
	}
	
	/**
	 * Test if any exceptions occurred
	 */
	public function isException() {
		return (! empty ( $this->exceptions ));
	}
	
	/**
	 * Append the current body
	 * @param unknown_type $string the data to be appended
	 */
	public function appendBody($string) {
		$this->body .= $string;
	}
	
	/**
	 * Clear the current body
	 */
	public function clear() {
		$this->body = "";
	}
}

?>